<?php
namespace app\api\controller;
use app\parent\api;
use think\Db;
use think\facade\Cache;

define('TABLE' , 'house_info');//二手房
define('TABLE_PLAT' , 'plat_info');//新盘
define('TABLE_LOUER' , 'louer_info');//出租房
define('TABLE_RESSOURCE' , 'ressources_info');//楼市资讯

class Detail extends Api{
	//新盘详情页面
	public function detail(){
		$id = $this->input('id' , 0);
	
		$data = Db::name(TABLE_PLAT)->where('id' , $id)->order('sort asc')->find();

		if(!empty($data['pic'])){
			if(strstr($data['pic'] , ',')){
				$data['pic'] = explode(',' , $data['pic']);
			}else{
				$data['pic'] = [$data['pic']];
			}
		}

		if(strstr($data['etiquette'] , ',')){
			$data['etiquette'] = explode(',' , $data['etiquette']);
		}else{
			$data['etiquette'] = [$data['etiquette']];
		}

		if(isset($data['prospectus'])){
			$str = strip_tags($data['prospectus']);
			$data['prospectus'] = str_replace('&nbsp;' , '' , $str);
		}
		//浏览量
		if(Cache::get('plat_' . $id)){
			$view = Cache::get('plat_' . $id);
			$view++;
			Cache::set('plat_' . $id , $view);
			if($view%10 == 0){
				Db::name(TABLE_PLAT)->where('id' , $id)->update(['view' => $view]);
			}
		}else{
			$view = Db::name(TABLE_PLAT)->where('id' , $id)->value('view');
			$view++;
			Db::name(TABLE_PLAT)->where('id' , $id)->update(['view' => $view]);
			Cache::set('plat_' . $id , $view);
		}

		$data['view'] = $view;
		echo json_encode($data);
		exit;
	}

	//二手房详情页面
	public function second(){
		$id = $this->input('id' , 0);
		
		$data = Db::name(TABLE)->where('id',$id)->find();
		// $data = Db::name(TABLE)
		// 	->alias('a')
		// 	->where('a.id',$id)
		// 	->join('zltc_house_user b','a.guan_id=b.id')
		// 	->find();
		$is = Db::name('house_user')->where('id',$data['guan_id'])->find();
		//图片
		if(!empty($data['pic'])){
			if(strstr($data['pic'] , ',')){
				$data['pic'] = explode(',' , $data['pic']);
			}else{
				$data['pic'] = [$data['pic']];
			}
		}

		// if(isset($data['detail'])){
		// 	$str = strip_tags($data['detail']);
		// 	$data['detail'] = str_replace('&nbsp;' , '' , $str);
		// }
		//标签
		if(strstr($data['etiquette'] , ',')){
			$data['etiquette'] = explode(',' , $data['etiquette']);
		}else{
			$data['etiquette'] = [$data['etiquette']];
		}

		//浏览量
		if(Cache::get('house_' . $id)){
			$view = Cache::get('house_' . $id);
			$view++;
			Cache::set('house_' . $id , $view);
			if($view%10 == 0){
				Db::name(TABLE)->where('id' , $id)->update(['view' => $view]);
			}
		}else{
			$view = Db::name(TABLE)->where('id' , $id)->value('view');
			$view++;
			Db::name(TABLE)->where('id' , $id)->update(['view' => $view]);
			Cache::set('house_' . $id , $view);
		}
		if($data['store'] == 1){
			$data['store'] = '有';
		}else{
			$data['store'] = '无';
		}
		if($data['carport'] == 1){
			$data['carport'] = '有';
		}else{
			$data['carport'] = '无';
		}
		
		$data['view'] = $view;
		return $this->ajaxReturn(["data"=>$data,"is"=>$is]);
		exit;
	}

	//出租房详情页面
	public function louer(){
		$id = $this->input('id' , 0);
		$data = Db::name('louer_info')->where('id',$id)->find();
		$is = Db::name('house_user')->where('id',$data['guan_id'])->find();
	
		// $data = Db::name('louer_info')
		// 	->alias('a')
		// 	->where('a.id',$id)
		// 	->join('zltc_house_user b','a.guan_id=b.id')
		// 	->find();
		//图片
		if(!empty($data['pic'])){
			if(strstr($data['pic'] , ',')){
				$data['pic'] = explode(',' , $data['pic']);
			}else{
				$data['pic'] = [$data['pic']];
			}
		}

		
		//标签
		if(strstr($data['etiquette'] , ',')){
			$data['etiquette'] = explode(',' , $data['etiquette']);
		}else{
			$data['etiquette'] = [$data['etiquette']];
		}

		// if(strstr($data['config'] , ',') && strstr($data['base'] , ',')){
		// 	$data['config'] = array_merge(explode(',' , $data['config']) , explode(',' , $data['base']));
		// }elseif(strstr($data['config'] , ',') && !strstr($data['base'] , ',')){
		// 	$arr = explode(',' , $data['config']);
		// 	array_push($arr , $data['base']);
		// 	$data['config'] = $arr;
		// }elseif(!strstr($data['config'] , ',') && !strstr($data['base'] , ',')){
		// 	$data['config'] = [$data['config'] , $data['base']];
		// }elseif(strstr($data['config'] , ',') && !strstr($data['base'] , ',')){
		// 	$arr = explode(',' , $data['base']);
		// 	array_push($arr , $data['config']);
		// 	$data['config'] = $arr;
		// }

		//类型
		if(strstr($data['type'] , ',')){
			$arr = explode(',' , $data['type']);
			$data['type'] = $arr[0];
			$data['vers'] = $arr[1];
		}
		if(!empty($data['desc'])){
			$data['desc'] = strip_tags($data['desc']);
		}
		//浏览量
		if(Cache::get('louer_' . $id)){
			$view = Cache::get('louer_' . $id);
			$view++;
			Cache::set('louer_' . $id , $view);
			if($view%10 == 0){
				Db::name(TABLE_LOUER)->where('id' , $id)->update(['view' => $view]);
			}
		}else{
			$view = Db::name(TABLE_LOUER)->where('id' , $id)->value('view');
			$view++;
			Db::name(TABLE_LOUER)->where('id' , $id)->update(['view' => $view]);
			Cache::set('louer_' . $id , $view);
		}
		if($data['store'] == 1){
			$data['store'] = '有';
		}else{
			$data['store'] = '无';
		}
		if($data['carport'] == 1){
			$data['carport'] = '有';
		}else{
			$data['carport'] = '无';
		}
		$data['view'] = $view;
		return $this->ajaxReturn(["data"=>$data,"is"=>$is]);
		// echo json_encode($data);
		exit;
	}

	//楼市资讯详情页面
	public function ressource(){
		$id = $this->input('id' , 0);
	
		$data = Db::name(TABLE_RESSOURCE)->where('id' , $id)
		->field('*,FROM_UNIXTIME(time,"%Y-%m-%d %h:%i:%s") as time')
		->find();

		// $data['content'] = strip_tags($data['content']);

		//浏览量
		if(Cache::get('res_' . $id)){
			$view = Cache::get('res_' . $id);
			$view++;
			Cache::set('res_' . $id , $view);
			if($view%10 == 0){
				Db::name(TABLE_RESSOURCE)->where('id' , $id)->update(['view' => $view]);
			}
		}else{
			$view = Db::name(TABLE_RESSOURCE)->where('id' , $id)->value('view');
			$view++;
			Db::name(TABLE_RESSOURCE)->where('id' , $id)->update(['view' => $view]);
			Cache::set('res_' . $id , $view);
		}

		$data['view'] = $view;
		echo json_encode($data);
		exit;
	}

	// 商铺详情
	public function store(){
		$id = $this->input('id' , 0);
		// $data = Db::name('house_store')
		// 	->alias('a')
		// 	->where('a.id',$id)
		// 	->join('zltc_house_user b','a.guan_id=b.id')
		// 	->find();
		$data = Db::name('house_store')->where('id',$id)->find();
		$is = Db::name('house_user')->where('id',$data['guan_id'])->find();
		//图片
		if(!empty($data['pic'])){
			if(strstr($data['pic'] , ',')){
				$data['pic'] = explode(',' , $data['pic']);
			}else{
				$data['pic'] = [$data['pic']];
			}
		}

		//标签
		if(strstr($data['etiquette'] , ',')){
			$data['etiquette'] = explode(',' , $data['etiquette']);
		}else{
			$data['etiquette'] = [$data['etiquette']];
		}

		//浏览量
		if(Cache::get('house_' . $id)){
			$view = Cache::get('house_' . $id);
			$view++;
			Cache::set('house_' . $id , $view);
			if($view%10 == 0){
				Db::name('house_store')->where('id' , $id)->update(['view' => $view]);
			}
		}else{
			$view = Db::name('house_store')->where('id' , $id)->value('view');
			$view++;
			Db::name('house_store')->where('id' , $id)->update(['view' => $view]);
			Cache::set('house_' . $id , $view);
		}

		// if(isset($data['desc'])){
		// 	$str = strip_tags($data['desc']);
		// 	$data['desc'] = str_replace('&nbsp;' , '' , $str);
		// }
		if($data['store'] == 1){
			$data['store'] = '有';
		}else{
			$data['store'] = '无';
		}
		if($data['carport'] == 1){
			$data['carport'] = '有';
		}else{
			$data['carport'] = '无';
		}
		$data['view'] = $view;
		return $this->ajaxReturn(["data"=>$data,"is"=>$is]);
		// echo json_encode($data);
		exit;
	}
	
	public function guanzhu(){
		$cid = $this->input('cid',0);
		$id = $this->input('id',0);
		$openid = $this->input('openid',0);
		if($cid == 3){
			$data = Db::name('house_store')->where(['id'=>$id,'cid'=>$cid])->find();
	
			$arr['house_id'] = $data['id'];
			$arr['cid'] = $data['cid'];
			$arr['position'] = $data['position'];
			$arr['pic'] = $data['pic'];
			$arr['prix'] = $data['prix'];
			$arr['etiquette'] = $data['etiquette'];
			$arr['aire'] = $data['aire'];
			$arr['openid'] = $openid;
			if($arr){
				$is = Db::name('house_guanzhu')->insert($arr);
				if($is){
					return $this->ajaxReturn(['msg'=>0,'id'=>$id],'操作成功');
				}else{
					return $this->ajaxReturn(1,'操作失败');
				}
			}
			
			
		}
		if($cid == 2){
			$data = Db::name('louer_info')->where(['id'=>$id,'cid'=>$cid])->find();
			
			$arr['house_id'] = $data['id'];
			$arr['cid'] = $data['cid'];
			$arr['position'] = $data['position'];
			$arr['pic'] = $data['pic'];
			$arr['prix'] = $data['prix'];
			$arr['etiquette'] = $data['etiquette'];
			$arr['aire'] = $data['aire'];
			$arr['openid'] = $openid;
			if($arr){
				$is = Db::name('house_guanzhu')->insert($arr);
				if($is){
					return $this->ajaxReturn(['msg'=>0,'id'=>$id],'操作成功');
				}else{
					return $this->ajaxReturn(1,'操作失败');
				}
			}
			
			
		}
		if($cid == 1){
			$data = Db::name('house_info')->where(['id'=>$id,'cid'=>$cid])->find();
			$arr['house_id'] = $data['id'];
			$arr['cid'] = $data['cid'];
			$arr['position'] = $data['position'];
			$arr['pic'] = $data['pic'];
			$arr['prix'] = $data['prix'];
			$arr['etiquette'] = $data['etiquette'];
			$arr['aire'] = $data['aire'];
			$arr['openid'] = $openid;
			if($arr){
				$is = Db::name('house_guanzhu')->insert($arr);
				if($is){
					return $this->ajaxReturn(['msg'=>0,'id'=>$id],'操作成功');
				}else{
					return $this->ajaxReturn(1,'操作失败');
				}
			}
			
		}
		if($cid == 4){
			$data = Db::name('plat_info')->where(['id'=>$id])->find();
			$arr['xinpan_id'] = $data['id'];
			$arr['etiquette'] = $data['etiquette'];
			$arr['cid'] = 4;
			$arr['title'] = $data['title'];
			$arr['pic'] = $data['pic'];
			$arr['openid'] = $openid;
			$arr['position'] = $data['position'];
			$arr['prix'] = $data['prix'];
			$arr['time'] = time();
			if($arr){
				$is = Db::name('house_xinpan')->insert($arr);
				if($is){
					return $this->ajaxReturn(['msg'=>0,'id'=>$id],'操作成功');
				}else{
					return $this->ajaxReturn(1,'操作失败');
				}
			}
		}
	}

	public function isguanzhu(){
		$cid = $this->input('cid',0);
		$id = $this->input('id',0);
		$openid = $this->input('openid',0);
		if($cid == 3){
			$is = Db::name('house_guanzhu')->where(['house_id'=>$id,'cid'=>$cid,'openid'=>$openid])->find();
			if($is){
		 		return	$this->ajaxReturn(['msg'=>1],'已收藏');
			}else{
				return	$this->ajaxReturn(['msg'=>0],'未收藏');
			}
		}
		if($cid == 2){
			$is = Db::name('house_guanzhu')->where(['house_id'=>$id,'cid'=>$cid,'openid'=>$openid])->find();
			if($is){
		 		return	$this->ajaxReturn(['msg'=>1],'已收藏');
			}else{
				return	$this->ajaxReturn(['msg'=>0],'未收藏');
			}
		}
		if($cid == 1){
			$is = Db::name('house_guanzhu')->where(['house_id'=>$id,'cid'=>$cid,'openid'=>$openid])->find();
			if($is){
		 		return	$this->ajaxReturn(['msg'=>1],'已收藏');
			}else{
				return	$this->ajaxReturn(['msg'=>0],'未收藏');
			}
		}
		if($cid == 4){
			$is = Db::name('house_xinpan')->where(['xinpan_id'=>$id,'cid'=>$cid,'openid'=>$openid])->find();
			if($is){
		 		return	$this->ajaxReturn(['msg'=>1],'已收藏');
			}else{
				return	$this->ajaxReturn(['msg'=>0],'未收藏');
			}
		}
	}

	public function quxiao(){
		$cid = $this->input('cid',0);
		$id = $this->input('id',0);
		$openid = $this->input('openid',0);
		if($cid == 3){
			$is = Db::name('house_guanzhu')->where(['house_id'=>$id,'cid'=>$cid,'openid'=>$openid])->delete();
			if($is){
				return $this->ajaxReturn(['msg'=>0,'id'=>$id],'操作成功');
			}
		}
		if($cid == 2){
			$is = Db::name('house_guanzhu')->where(['house_id'=>$id,'cid'=>$cid,'openid'=>$openid])->delete();
			if($is){
				return $this->ajaxReturn(['msg'=>0,'id'=>$id],'操作成功');
			}
		}
		if($cid == 1){
			$is = Db::name('house_guanzhu')->where(['house_id'=>$id,'cid'=>$cid,'openid'=>$openid])->delete();
			if($is){
				return $this->ajaxReturn(['msg'=>0,'id'=>$id],'操作成功');
			}
		}
		if($cid == 4){
			$is = Db::name('house_xinpan')->where(['xinpan_id'=>$id,'cid'=>$cid,'openid'=>$openid])->delete();
			if($is){
				return $this->ajaxReturn(['msg'=>0,'id'=>$id],'操作成功');
			}
		}
	}

	//记录访问人
	public function jilu(){
		$openid = $this->input('openid',0);
		$id = $this->input('guan_id',0);
		$shop_id = $this->input('shop_id',0);
		$cid = $this->input('cid',0);
		$data = Db::name('house_user')
			->where(['x_openid'=>$openid])
			->field('pic_user,nickname,id')
			->find();
		$res['id_user'] = $data['id'];
		$res['name_user'] = $data['nickname'];
		$res['pic_user'] = $data['pic_user'];
		$res['guan_id'] = $id;
		$res['shop_id'] = $shop_id;
		$res['time'] = time();
		if($cid == 1){
			$res['cid_house'] = "二手房";
		}else if($cid == 2){
			$res['cid_house'] = "出租房";
		}else if($cid ==3){
			$res['cid_house'] = "商铺";
		}

		$shu = Db::name('house_jilu')
			->where(['shop_id'=>$shop_id,'cid_house'=>$res['cid_house'] ])
			->find();
		if($shu){
			return '';
		}else{
			$is = Db::name('house_jilu')->insert($res);
			if($is){
				return $this->ajaxReturn(0,'操作成功');
			}else{
				return $this->ajaxReturn(1,'操作失败');
			}
		}
	}
}